package com.windchime.blog.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.windchime.blog.entity.Menu;
import com.windchime.blog.entity.vo.menu.Child;
import com.windchime.blog.entity.vo.menu.HomeInfo;
import com.windchime.blog.entity.vo.menu.LogoInfo;
import com.windchime.blog.entity.vo.menu.MenuList;
import com.windchime.blog.result.R;
import com.windchime.blog.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author WindChime
 * @since 2021-08-19
 */
@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 后台菜单
     * @return menu
     */
    @GetMapping("/menuList")
    public MenuList menuList() {
        MenuList menuList = new MenuList();

        Menu home = menuService.getOne(new QueryWrapper<Menu>().eq("title", "首页").eq("sort", 0));
        HomeInfo homeInfo = new HomeInfo();
        BeanUtils.copyProperties(home, homeInfo);
        menuList.setHomeInfo(homeInfo);

        Menu logo = menuService.getOne(new QueryWrapper<Menu>().eq("title", "风铃博客").eq("sort", 0));
        LogoInfo logoInfo = new LogoInfo();
        BeanUtils.copyProperties(logo, logoInfo);
        logoInfo.setImage("/layuimini/images/logo.png");
        menuList.setLogoInfo(logoInfo);

        List<Menu> menus = menuService.list(new QueryWrapper<Menu>().eq("title", "后台管理").eq("sort", 0));
        List<Child> children = menus.stream().map(menu -> {
            Child child = new Child();
            BeanUtils.copyProperties(menu, child);
            return child;
        }).collect(Collectors.toList());
        for (Child child : children) {
            List<Menu> oneMenu = menuService.list(new QueryWrapper<Menu>().eq("sort", 1));
            List<Child> oneChile = oneMenu.stream().map(one -> {
                Child c = new Child();
                BeanUtils.copyProperties(one, c);
                return c;
            }).collect(Collectors.toList());

            for (Child one : oneChile) {
                List<Menu> twoMenu = menuService.list(new QueryWrapper<Menu>().eq("sort", 2).likeRight("title", one.getTitle().substring(0, one.getTitle().length() - 2)));
                List<Child> twoChild = twoMenu.stream().map(two -> {
                    Child c = new Child();
                    BeanUtils.copyProperties(two, c);
                    return c;
                }).collect(Collectors.toList());
                one.setChild(twoChild);
            }
            child.setChild(oneChile);
        }
        menuList.setMenuInfo(children);

        return menuList;
    }



}

